// 真实的后端认证服务
import apiService from './apiService';

export const authService = {
    async register(userData) {
        const result = await apiService.auth.register(userData);
        if (result.success) {
            return {
                user: {
                    username: result.data.username,
                    userId: result.data.userId,
                    email: result.data.email,
                    role: result.data.role
                },
                token: result.data.token
            };
        }
        throw new Error(result.message || '注册失败');
    },

    async login(credentials) {
        const result = await apiService.auth.login(credentials);
        if (result.success) {
            return {
                user: {
                    username: result.data.username,
                    userId: result.data.userId,
                    email: result.data.email,
                    role: result.data.role,
                    createdAt: result.data.createdAt,
                    lastLogin: result.data.lastLogin,
                    status: result.data.status,
                    vipLevel: result.data.vipLevel
                },
                token: result.data.token
            
            };
        }
        throw new Error(result.message || '登录失败');
    },

    async resetPassword(email) {
        const result = await apiService.auth.resetPassword(email);
        if (result.success) {
            return result;
        }
        throw new Error(result.message || '重置密码失败');
    },

    async validateToken(token) {
        const result = await apiService.auth.getProfile();
        if (result.success) {
            return {
                username: result.data.username,
                userId: result.data.userId,
                email: result.data.email,
                role: result.data.role,
                status: result.data.status,
                lastLogin: result.data.lastLogin,
                createdAt: result.data.createdAt
            };
        }
        return null;
    },

    async updateProfile(userId, updates) {
        const result = await apiService.auth.updateProfile(updates);
        if (result.success) {
            return result.data;
        }
        throw new Error(result.message || '更新资料失败');
    }
};